layui.use(['jquery', 'form', 'layer'], function() {
	var $ = layui.$;
	var form = layui.form;
	var layer = layui.layer;
	//登录
	function login() {
		var username = document.getElementById('userName').value;
		var pas = document.getElementById('pas').value;
		if(username && pas) {
			$.ajax({
				type: "post",
				url: "/login",
				async: true,
				data: {
					username: username,
					pas: pas
				},
				success: function(data) {
					if(data.code == "0") {
						location.href = "/home";
						localStorage.setItem("username", username);
					} else {
						layer.msg(data.message);
					}
				}
			});
		} else {
			layer.msg("用户名或密码不能为空！")
		}
	}

	$("#loginBtn").on("click", function() {
		login();
	});
	$("body").on("keydown", function(e) {
		var keycode;
		if(window.event) {
			keycode = e.keyCode;
		} else if(e.which) {
			keycode = e.which;
		}

		if(keycode == 13) {
			login();
		}
	});
});